<template>
  <div class="bodyBox">
    <FrameStyle>
      <!-- 面包屑 -->
      <Breadcrumb :breadName="'扶贫政策'" slot="breadcrumb" />

      <!-- 搜索 -->
      <el-input slot="search">
        <el-button
          type="primary"
          class="el-icon-search"
          slot="append"
        ></el-button>
      </el-input>

      <!-- 添加政策 -->
      <el-button type="primary" slot="addBtn" @click="addDiaLogVisible = true"
        >添加政策</el-button
      >

      <!-- 表格列表 -->
      <el-table :data="tableData" slot="tableList" height="370px" stripe>
        <el-table-column label="创建时间" prop="createTime" width="120px">
        </el-table-column>
        <el-table-column
          prop="title"
          label="标题"
          show-overflow-tooltip
          width="180px"
        ></el-table-column>
        <el-table-column
          prop="type"
          label="扶贫类别"
          width="100px"
        ></el-table-column>
        <el-table-column label="内容" prop="content" width="auto">
          <template slot-scope="scope">
            <span class="contentText">{{ scope.row.content }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="cover" label="封面" width="100px">
          <template slot-scope="scope">
            <img
              :src="scope.row.cover"
              style="width: 70px"
              @click="lookImg(scope.row.cover)"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="clickNum"
          label="点击次数"
          width="100px"
        ></el-table-column>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <el-button size="mini" @click="openChangeDiaLog(scope.row)"
              >修改</el-button
            >
            <el-button
              type="danger"
              size="mini"
              @click="delAlleviation(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <Pagination
        slot="pageSlot"
        :pageNo="pageNum"
        :pageSize="pageSize"
        :totalRows="totalRows"
        @pageChange="pageChange"
      />
    </FrameStyle>
    <el-dialog :visible.sync="flag">
      <el-image :src="diaLogImg" fit="cover"></el-image>
    </el-dialog>
    <el-dialog :visible.sync="addDiaLogVisible">
      <el-form :model="addForm">
        <el-form-item label="标题">
          <el-input v-model="addForm.title"></el-input>
        </el-form-item>
        <el-form-item label="扶贫类型">
          <el-input v-model="addForm.type"></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="addForm.content"></el-input>
        </el-form-item>
        <el-form-item label="发布时间">
          <el-input v-model="addForm.releaseTime" disabled></el-input>
        </el-form-item>
        <el-form-item label="封面">
          <UploadImg :type="`ALLEVIATION`" @getImgUrl="getImgUrl" />
        </el-form-item>
        <el-form-item label="点击次数" >
          <el-input v-model="addForm.clickNum" disabled></el-input>
        </el-form-item>
      </el-form>
      <div>
        <el-button type="primary" @click="addAlleviation">确定</el-button>
        <el-button>重置</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="changeDiaLogVisible">
      <el-form :model="changeForm" ref="ruleForm">
        <el-form-item label="标题" prop="title">
          <el-input v-model="changeForm.title"></el-input>
        </el-form-item>
        <el-form-item label="扶贫类型" prop="type">
          <el-input v-model="changeForm.type"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input v-model="changeForm.content"></el-input>
        </el-form-item>
        <el-form-item label="发布时间" prop="releaseTime">
          <el-input v-model="changeForm.releaseTime" disabled></el-input>
        </el-form-item>
        <el-form-item label="封面" prop="cover">
          <UploadImg :type="`ALLEVIATION`" @getImgUrl="getImgUrl" />
        </el-form-item>
      </el-form>
      <div>
        <el-button type="primary" @click="changeAlleviation">确定</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  reqGetFuPinList,
  reqAddData,
  reqDelData,
  reqChangeData,
} from "../../../api";
export default {
  data() {
    return {
      tableData: [],
      addForm: {
        clickNum:0
      },
      imgUrl: "",
      changeForm: {},
      pageNum: 1,
      pageSize: 10,
      totalRows: 1,
      flag: false,
      diaLogImg: "",
      addDiaLogVisible: false,
      changeDiaLogVisible: false,
    };
  },
  created() {
    this.getData();
  },
  methods: {
    //获取扶贫政策数据
    async getData() {
      let { pageNum, pageSize } = this;
      let result = await reqGetFuPinList({ pageNum, pageSize });
      if (result.code == 200) {
        result.data.list.forEach((item) => {
          item.createTime = this.$format(item.createTime);
        });
        this.tableData = result.data.list;
        this.totalRows = result.data.total;
      }
    },
    //添加扶贫政策数据
    async addAlleviation() {
      this.addForm.cover = this.imgUrl;
      let result = await reqAddData("/alleviation/add", this.addForm);
      if (result.code != 200) return this.$message.error("添加失败");
      this.$message({ message: "添加成功", type: "success" });
      this.getData();
      this.addDiaLogVisible = false;
    },
    //删除扶贫政策数据
    async delAlleviation(id) {
      let result = await reqDelData("/alleviation/delete", id);
      if (result.code != 200) return this.$message.error("删除失败");
      this.$message({ message: "删除成功", type: "success" });
      this.getData();
    },
    //打开编辑弹出层
    openChangeDiaLog(row) {
      this.changeDiaLogVisible = true;
      delete row.createTime
      this.$nextTick(()=>{
        this.changeForm = row;
      })
    },
    //重置表单
    resetForm(formName){
      console.log(6);
      this.$refs[formName].resetFields()
    },
    //扶贫编辑回调
    async changeAlleviation() {
      this.imgUrl != "" ? (this.changeForm.cover = this.imgUrl) : false;
      let result = await reqChangeData("/alleviation/update", this.changeForm);
      if (result.code != 200) return this.$message.error("修改失败");
      this.$message({ message: "修改成功", type: "success" });
      this.getData();
      this.changeDiaLogVisible = false;
    },
    //查看图片
    lookImg(img) {
      this.diaLogImg = img;
      this.flag = true;
    },
    //分页回调
    pageChange(page) {
      this.pageNum = page;
      this.getData();
    },
    getImgUrl(url) {
      this.imgUrl = url;
    },
  },
};
</script>


<style lang="less" scoped>
.contentText {
  display: -webkit-box !important;
  -webkit-line-clamp: 2 !important;
  -webkit-box-orient: vertical !important;
}
</style>